/**
 * @author JeffYoung
 * @create 2017年3月24日
 */
package com.scau.action;

import java.util.List;

import net.sf.json.JSONArray;

import org.apache.log4j.Logger;

import com.scau.dao.impl.VipDaoImpl;
import com.scau.service.IVipService;
import com.scau.vo.Vip;

public class VipAction extends SuperAction {
	public static final Logger logger = Logger.getLogger(VipAction.class);
	
	private String result;// 属性result，用于异步传输数据交互
	
	private IVipService vipService;
	private Vip vip;
	
	//添加vip用户
	public String saveVip(){
		Vip vip = getVip();
		System.out.println("添加的vip为：" + vip.getName());
		if (vipService.saveVip(vip)) {
			List<Vip> vipsList = vipService.listVip();
			JSONArray jsonArray = JSONArray.fromObject(vipsList.toString());
			result = jsonArray.toString();
			return "success";
		} else
			return "error";
	}
	
	//删除vip用户
	public String deleteVip(){
		Vip vip = getVip();
		System.out.println("要删除的vip为：" + vip.getName());
		if (vipService.deleteVip(vip.getId())) {
			List<Vip> vipsList = vipService.listVip();
			JSONArray jsonArray = JSONArray.fromObject(vipsList.toString());
			result = jsonArray.toString();
			return "success";
		} else
			return "error";
	}
	
	public String listVip(){
		String url = VipAction.class.getClassLoader().getResource("").getPath();
		url = url.replace("WEB-INF/classes/","log/");
		System.setProperty("LOG_DIR",url);
		
		
		List<Vip> vipsList = vipService.listVip();
		logger.info("查出所有会员信息");
		JSONArray jsonArray = JSONArray.fromObject(vipsList.toString());
		result = jsonArray.toString();
		logger.info("以json格式返回所有查询的会员信息");
		System.out.println("url:" + url);
		return "success";
	}
	
	/*//模糊查询
	public String selectVips(){
		list = vipService.selectVips(getMessage());
		System.out.println("查出的vip列表:" + list);
		if(list != null){
			return "success";
		}else 
			return "error";
	}*/
	
	//修改vip信息
	public String updateVip(){
		Vip vip = getVip();
		System.out.println("更新的vip为："+vip.getName());
		if (vipService.updateVip(vip)) {
			List<Vip> vipsList = vipService.listVip();
			JSONArray jsonArray = JSONArray.fromObject(vipsList.toString());
			result = jsonArray.toString();
			return "success";
		} else
			return "error";
	}
	
	// 根据ID查找一条vip记录
	public String getOneVip(){
		Vip vip = getVip();
		
		List<Vip> foundVip = vipService.getOneVip(vip.getId());
		JSONArray jsonArray = JSONArray.fromObject(foundVip.toString());
		result = jsonArray.toString();
		return "success";
	}
	
	// 充值操作
	public String rechargeForVip(){
		Vip vip = getVip();
		List<Vip> rechVip = vipService.rechargeForVip(vip);
		JSONArray jsonArray  =JSONArray.fromObject(rechVip.toString());
		result = jsonArray.toString();
		return "success";
	}
	
	// 储值卡余额更新操作
	public String changeVipAmount(){
		Vip vip = getVip();
		List<Vip> changeAmountVip = vipService.changeVipAmount(vip);
		JSONArray jsonArray = JSONArray.fromObject(changeAmountVip.toString());
		result = jsonArray.toString();
		return "success";
	}
	

	// get,set methods
	public IVipService getVipService() {
		return vipService;
	}

	public void setVipService(IVipService vipService) {
		this.vipService = vipService;
	}

	public Vip getVip() {
		return vip;
	}

	public void setVip(Vip vip) {
		this.vip = vip;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	
}
